﻿<?php
/**
 * AdminController
 * 
 * @category   Longadmin
 * @package    Longadmin_Controllers
 */

class Longadmin_NewsController extends Zend_Controller_Action 
{
	public function newsAction()
	{
		$form = new Form_NewsForm();
		$form->setAction('news')
			  ->setMethod('post');
		if($this->_request->isPost()){
			if($form->image->receive()){
				$_IMAGE = $form->image->getFileName(null,false); 
			}
			$modelNews = new Model_News();
			
			$modelNews->createNews($_POST['n_tieude'],$_POST['n_noidung'],$_IMAGE);
			$this->view->form = " Đã tạo xong tin mới ";	
		}
		else
		{
			$this->view->form = $form;
		}
	}
	/*public function  viewAction()  {
		$viewModels=new Model_News();
		
		$adapter=$viewModels->fetchPaginatorAdapter();
		$paginator=new Zend_Paginator($adapter);
		//show 10 news per page
		$paginator->setItemCountPerPage(10);
		//get the page number that is passed in the request
		$page=$this->_request->getParam('page',1);
		$paginator->setCurrentPageNumber($page);
		//
		$this->view->paginator=$paginator;
	}	*/
	public function viewpostAction()  {
	
		$viewModels=new Model_News();
		$paginator=$viewModels->orderViewNews();
		/*$adapter=$viewModels->fetchPaginatorAdapter();
		$paginator=new Zend_Paginator($adapter);
		//show 10 news per page
		$paginator->setItemCountPerPage(10);
		//get the page number that is passed in the request
		$page=$this->_request->getParam('page',1);
		$paginator->setCurrentPageNumber($page);
		//*/
		$this->view->paginator=$paginator;   
	}
	public function viewnewsAction() {
	
		$id = (int)$this->getRequest()->getParam('n_id');
		$viewNews = new Model_News();
		$views = $viewNews->getNewsDetails($id);
		$this->view->viewnews=$views;
		$viewNewsTitle = new Model_News();
		$viewTitle=$viewNewsTitle->getViewNews($id);
		$this->view->viewNewTitle=$viewTitle;
		
	     
	}

	public function listAction()
	{
		$rp = $_POST['rp'];
		$page = $_POST['page'];
		$pageStart = ($page-1)*$rp;
		$newsModel = new Model_News();
		$totalarray = $newsModel->getNewsAll();
		$result= $newsModel->getNewsLimit($rp,$pageStart);
		$test = array();
		$i=0;
		
		foreach($result as $x)
		{
			$test[$i]['n_id'] = "".$x['n_id']."";			
			$test[$i]['n_tieude'] = "".$x['n_tieude']."";
			$test[$i]['n_noidung'] = "".$x['n_noidung']."";	
			$test[$i]['n_ngay'] = "".$x['n_ngay']."";				
			$test[$i]['n_anhdd'] = "<input type='image' src='/public/images/news/"	.$x['n_anhdd']. "' width='40' height='40'/>";
			$i++;
		}

		$data = array();
		$data['page'] = $page;
		$data['total'] = count($totalarray);
		foreach ($test as $x) {
			$data['rows'][] = array(
			'id' => $x['n_id'],
			'cell' => array($x['n_id'],$x['n_tieude'], $x['n_ngay'],$x['n_anhdd']));
		}
		
		$this->_helper->viewRenderer->setNoRender();
		echo json_encode($data);		

	}
	
	public function deleteAction()
	{
		$id = $_GET['id'];
		
		$modelNews = new Model_News();
		$modelNews->deleteNewsById($id);
		$this->view->id = $id;
	}
	
	public function updateAction()
	{
		$n_id = $_GET['id'];
		$modelNews = new Model_News();
		$modelNews->getNewsById($n_id);
		$form = new Form_NewsForm();
		$modelNews = new Model_News();
		
		if($this->_request->isPost())
		{
			$n_tieude = $_POST['n_tieude'];
			$n_noidung = $_POST['n_noidung'];
			$modelNews->updateNews($n_id,$n_tieude,$n_noidung);
		}
	}	
	
}
